<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="java.sql.DriverManager"%>
<%
    String msg = "";
    String sql=request.getParameter("sql");
    String tipo=request.getParameter("tipo");
    if(tipo==null){tipo="";}
    if(sql==null)sql="";    
    sql=sql.trim(); 
    response.getWriter().println("<h1>Version: 14-06-2012::18:00</h1>");
	response.getWriter().println("<form method='post'>");
	response.getWriter().println("<textarea style='width:100%; height:150px' name='sql'>");
	response.getWriter().println(sql);
	response.getWriter().println("</textarea>");
	response.getWriter().println("<br><input type='submit' value='Ejecutar>>'/>");
	if(tipo.equals("1")){response.getWriter().println("<input type='checkbox' name='tipo' checked='checked'  value='1'>");}
	else{response.getWriter().println("<input type='checkbox' name='tipo' value='1'>");}
	response.getWriter().println("</form>");
	//response.getWriter().println("<hr>"); 
    if(sql.length()>0){
      java.sql.Connection cn=null;
       try {
		    cn = ((DataSource) new InitialContext().lookup("jdbc/SEGAPP")).getConnection();
 
		        String[] asql=sql.split(";");
		        for(int i=0;i<asql.length;i++){
		          response.getWriter().println("<hr>");          
		          response.getWriter().println("<font color='#0000FF'>sql"+i+">"+asql[i]+"</font><br>");
		          asql[i]=asql[i].trim(); 
		          if(asql[i].indexOf("select")==0){
		              java.sql.ResultSet rs = cn.createStatement().executeQuery(asql[i]);
		                response.getWriter().println("<table  border='1'>");
		                response.getWriter().println("<tr>");
		                response.getWriter().println("<th></th>");
		                for(int j=1;j<=rs.getMetaData().getColumnCount();j++){
		                    response.getWriter().println("<td>");
		                    response.getWriter().println(rs.getMetaData().getColumnTypeName(j));
		                    if(!rs.getMetaData().getColumnTypeName(j).equals("BLOB"))
		                        response.getWriter().println("("+rs.getMetaData().getPrecision(j)+","+rs.getMetaData().getScale(j)+")");
		                    response.getWriter().println("</td>");
		                }
		                response.getWriter().println("</tr>");
		                response.getWriter().println("<tr>");
		                response.getWriter().println("<th>NroFila</th>");
		                for(int j=1;j<=rs.getMetaData().getColumnCount();j++){
		                    response.getWriter().println("<th>");
		                    response.getWriter().println(rs.getMetaData().getColumnName(j));
		                    response.getWriter().println("</th>");
		                }
		                response.getWriter().println("</tr>"); 
		               while (rs.next()) {
		                response.getWriter().println("<tr>");
		                response.getWriter().println("<td>"+rs.getRow()+"</td>");
		                for(int j=1;j<=rs.getMetaData().getColumnCount();j++){
		                   if(!rs.getMetaData().getColumnTypeName(j).equals("BLOB"))
		                    response.getWriter().println("<td>"+rs.getString(j)+"</td>");
		                   else
		                    response.getWriter().println("<td>...</td>");
		                }
		                response.getWriter().println("</tr>");
		              }
		              response.getWriter().println("</table>");
		          }else{
		              cn.createStatement().execute(asql[i]);
		              response.getWriter().println("Ejecutado OK!Z<br>");
		          }
		        }
  
    } catch (Exception e) {
      response.getWriter().println("<h3>Error de BD!!!</h3>");
      response.getWriter().println("<hr>");
      response.getWriter().println("<font color='#FF0000'>"+e.getMessage()+"</font>");
    }finally{
          if (cn!=null) cn.close();
    }
    }else{
          response.getWriter().println("Ninguna instruccion!<br>");
    } 
%>
